package com.changgou.order.comsumer;

import com.alibaba.fastjson.JSON;
import com.changgou.order.service.OrderService;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @ClassName OrderPayMessageListener
 * @Description 消息监听
 * @Author Mr.L
 * @Date 21:14 2019/11/26
 * @Version 2.1
 **/
@Component
@RabbitListener(queues = "${mq.pay.queue.order}")
public class OrderPayMessageListener {

    @Autowired
    private OrderService orderService;

    @RabbitHandler
    public void readMessage(String msg){
        Map<String, String> map = JSON.parseObject(msg, Map.class);
        // 通信标识
        String return_code = map.get("return_code");        // 通信标识
        if ("SUCCESS".equals(return_code)){
            String result_code = map.get("result_code");    // 交易标识
            String out_trade_no = map.get("out_trade_no");
            if ("SUCCESS".equals(result_code)){
                // 支付成功，更新订单
                String transaction_id = map.get("transaction_id");
                orderService.updateOrder(out_trade_no, transaction_id);
            }else {
                // 支付失败，删除订单
                orderService.deleteOrder(out_trade_no);
            }
        }
    }
}
